<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Game
 *
 * @property int $id
 * @property int $current_row 当前环数
 * @property int $row_status 当前环状态（0-未开启 1-已开启）
 * @property int $game_status 游戏状态（0-已结束 1-进行中）
 * @property int $node 当前环节点数
 * @property int $remain_node 当前环剩余节点数
 * @property string $token 当前环产币数量
 * @property string $price 当前环回购价格
 * @property \Illuminate\Support\Carbon $created_at 游戏创建时间
 * @property string $update_at 游戏修改时间
 * @property string|null $finish_at 游戏结束时间
 * @method static \Illuminate\Database\Eloquent\Builder|Game newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Game newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Game query()
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereCurrentRow($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereFinishAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereGameStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereNode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereRemainNode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereRowStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Game whereUpdateAt($value)
 * @mixin \Eloquent
 */
class Game extends Model
{
//    public $timestamps = false;

    protected $table = 'games';

    /**
     * 查询指定环的节点数、产币量、回购价格
     * @param $num
     * @return array
     */
    public static function getRow($num)
    {
        // 节点数
        $node = 0;
        if ($num == 1) {
            $node = 1;
        } else {
            $node = 2 * $num - 2;
        }

        // 产币量
        $token = 20 * (2 * $num - 2) / (0.15 + 0.02 * ($num - 2));
        $token = round($token, 6);

        // 回购价格
        $price = 0;
        if ($num == 1) {
            $price = 0;
        } else if ($num == 2) {
            $price = 0.15;
        } else if ($num >= 3) {
            $price = bcadd(0.15, bcmul(0.02, ($num -2), 2), 2);
        }

        return compact('node', 'token', 'price');
    }

}
